<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>去规范化你的数据 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/denormalization.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/denormalization.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/denormalization.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/denormalization.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="modeling-your-data.html">数据建模</a></span>
»
<span class="breadcrumb-link"><a href="relations.html">关联关系处理</a></span>
»
<span class="breadcrumb-node">去规范化你的数据</span>
</div>
<div class="navheader">
<span class="prev">
<a href="application-joins.html">« 应用层联接</a>
</span>
<span class="next">
<a href="top-hits.html">字段折叠 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="denormalization"></a>去规范化你的数据 (Denormalizing Your Data)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/400_Relationships/20_Denormalization.asciidoc">edit</a>
</h2>
</div></div></div>
<p>使用 Elasticsearch 得到最好的搜索性能的方法是有目的的通过在索引时进行 
<a href="http://en.wikipedia.org/wiki/Denormalization" class="ulink" target="_top">去规范化(denormalizing)</a>。对每个文档保持一定数量的冗余副本可以在需要访问时避免进行关联。</p>
<p>如果我们希望能够通过某个用户姓名找到他写的博客文章，可以在博客文档中包含这个用户的姓名：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /my_index/user/1
{
  "name":     "John Smith",
  "email":    "john@smith.com",
  "dob":      "1970/10/24"
}

PUT /my_index/blogpost/2
{
  "title":    "Relationships",
  "body":     "It's complicated...",
  "user":     {
    "id":       1,
    "name":     "John Smith" <a id="CO250-1"></a><i class="conum" data-value="1"></i>
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO250-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>这部分用户的字段数据已被冗余到 <code class="word">blogpost</code> 文档中。</p>
</td>
</tr>
</table>
</div>
<p>现在，我们通过单次查询就能够通过 <code class="literal">relationships</code> 找到用户 <code class="word">John</code> 的博客文章。</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /my_index/blogpost/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title":     "relationships" }},
        { "match": { "user.name": "John"          }}
      ]
    }
  }
}</pre>
</div>
<p>数据去规范化的优点是速度快。因为每个文档都包含了所需的所有信息，当这些信息需要在查询进行匹配时，并不需要进行昂贵的联接操作。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="application-joins.html">« 应用层联接</a>
</span>
<span class="next">
<a href="top-hits.html">字段折叠 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>